package com.vast.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vast.admin.entity.ContractDetailSigner;
import com.vast.admin.mapper.ContractDetailSignerMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ContractDetailSignerService extends ServiceImpl<ContractDetailSignerMapper, ContractDetailSigner> {

    /**
     * 通过合同详情Id删除
     * @param contractDetailId
     */
    public void deleteByContractDetailId(Long contractDetailId) {
        QueryWrapper<ContractDetailSigner> contractDetailSignerQueryWrapper = new QueryWrapper<>();
        contractDetailSignerQueryWrapper.eq(ContractDetailSigner.COL_CONTRACT_DETAIL_ID, contractDetailId);
        super.remove(contractDetailSignerQueryWrapper);
    }

    /**
     *
     * @return
     */
    public List<ContractDetailSigner> getContractDetailSigners(Long contractId, Long contractDetailId, Integer type) {
        QueryWrapper<ContractDetailSigner> contractDetailSignerQueryWrapper = new QueryWrapper<>();
        contractDetailSignerQueryWrapper.eq(ContractDetailSigner.COL_CONTRACT_ID, contractId)
                .eq(ContractDetailSigner.COL_CONTRACT_DETAIL_ID, contractDetailId)
                .eq(ContractDetailSigner.COL_PART_TYPE, type);
        return super.list(contractDetailSignerQueryWrapper);
    }

    /**
     *
     * @return
     */
    public List<ContractDetailSigner> getByContractId(Long contractId) {
        QueryWrapper<ContractDetailSigner> contractDetailSignerQueryWrapper = new QueryWrapper<>();
        contractDetailSignerQueryWrapper.eq(ContractDetailSigner.COL_CONTRACT_ID, contractId);
        return super.list(contractDetailSignerQueryWrapper);
    }

    /**
     *
     * @return
     */
    public List<ContractDetailSigner> getByContractDetailId(Long contractDetailId) {
        QueryWrapper<ContractDetailSigner> contractDetailSignerQueryWrapper = new QueryWrapper<>();
        contractDetailSignerQueryWrapper.eq(ContractDetailSigner.COL_CONTRACT_DETAIL_ID, contractDetailId);
        return super.list(contractDetailSignerQueryWrapper);
    }

    /**
     * 变更为已签署，添加签署时间
     * @param id
     */
    public void signed(Long id) {
        ContractDetailSigner contractDetailSigner = new ContractDetailSigner();
        contractDetailSigner.setId(id);
        contractDetailSigner.setSignStatus(1);
        contractDetailSigner.setSignTime(new Date());
        super.updateById(contractDetailSigner);
    }

    /**
     * 变更为已签署，添加签署时间
     * @param contractId
     * @param customerId
     */
    public void signed(Long contractId, Long customerId) {
        UpdateWrapper<ContractDetailSigner> contractDetailSignerUpdateWrapper = new UpdateWrapper<>();
        contractDetailSignerUpdateWrapper.set(ContractDetailSigner.COL_SIGN_STATUS, 1)
                .set(ContractDetailSigner.COL_SIGN_TIME, new Date())
                .eq(ContractDetailSigner.COL_CONTRACT_ID, contractId)
                .eq(ContractDetailSigner.COL_SIGNER_ID, customerId);
        super.update(contractDetailSignerUpdateWrapper);
    }

    /**
     * 判断是否已设置签署者
     * @param contractDetailId
     * @param partName
     * @return
     */
    public boolean isContainSigner(Long contractDetailId, String partName) {
        QueryWrapper<ContractDetailSigner> contractDetailSignerQueryWrapper = new QueryWrapper<>();
        contractDetailSignerQueryWrapper.eq(ContractDetailSigner.COL_CONTRACT_DETAIL_ID, contractDetailId)
                .eq(ContractDetailSigner.COL_PART_NAME, partName);
        return super.count(contractDetailSignerQueryWrapper) > 0;
    }

}
